package com.example.androidproject.novel.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.androidproject.R;
import com.example.androidproject.myview.RoundCornerImageView;
import com.example.androidproject.novel.adapter.NovelCatalogueAdapter;
import com.example.androidproject.novel.bean.CatalogBean;
import com.example.androidproject.novel.bean.NovelBean;
import com.example.androidproject.retrofit.RetrofitService;
import com.example.androidproject.utils.RequestConstant;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CatalogueActivity extends Activity {
    final String TAG = "GGSTUDY";
    private NovelBean.DataBean data;
    private RoundCornerImageView iv_novel_content;
    private TextView tv_novel_title, tv_novel_introduce, tv_novel_author, tv_novel_num;
    private RecyclerView rv_novel_catalogue;
    private Retrofit retrofit;
    private List<String> CatalogList;
    private NovelCatalogueAdapter novelCatalogueAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_catalogue);
        //设置顶部状态栏为透明
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
        Intent intent = getIntent();
        data = intent.getParcelableExtra("data");

        retrofit = new Retrofit.Builder().baseUrl(RequestConstant.BASE).addConverterFactory(GsonConverterFactory.create()).build();

        initView();
        initPage();
        getCatalog();
    }

    private void initView() {
        iv_novel_content = findViewById(R.id.iv_novel_content);
        tv_novel_title = findViewById(R.id.tv_novel_title);
        tv_novel_introduce = findViewById(R.id.tv_novel_introduce);
        tv_novel_author = findViewById(R.id.tv_novel_author);
        tv_novel_num = findViewById(R.id.tv_novel_num);
        rv_novel_catalogue = findViewById(R.id.rv_novel_catalogue);
    }

    private void initPage() {
        Glide.with(this).load(data.getImage()).into(iv_novel_content);
        tv_novel_title.setText(data.getTitle());
        tv_novel_introduce.setText(data.getInfo());
        tv_novel_author.setText(data.getAuthor());
        tv_novel_num.setText(data.getNumber());

        novelCatalogueAdapter = new NovelCatalogueAdapter(getApplicationContext(), CatalogList);
        rv_novel_catalogue.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        rv_novel_catalogue.setAdapter(novelCatalogueAdapter);

        // 点击
        novelCatalogueAdapter.setItemOnClickListener(new NovelCatalogueAdapter.ItemOnClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(getApplicationContext(), NovelContentActivity.class);
                Bundle bundle = new Bundle();
                bundle.putParcelable("data", data);
                bundle.putString("catatitle", CatalogList.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }

    private void getCatalog() {
        String novelname = data.getTitle();
        String classify = data.getClassify();
        String url = RequestConstant.NOVEL_CONTENT_URL + "?novelname=" + novelname + "&classify=" + classify;
        RetrofitService retrofitService = retrofit.create(RetrofitService.class);
        Call<CatalogBean> catalogCall = retrofitService.getCatalog(url);
        catalogCall.enqueue(new Callback<CatalogBean>() {
            @Override
            public void onResponse(Call<CatalogBean> call, Response<CatalogBean> response) {
                if (response.isSuccessful()) {
                    CatalogBean body = response.body();
                    assert body != null;
                    CatalogList = body.getData();
                    novelCatalogueAdapter.refresh(body.getData());
                } else {
                }
            }

            @Override
            public void onFailure(Call<CatalogBean> call, Throwable t) {

            }
        });
    }
}